package org.yaac.shared.property;

import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.newLinkedList;

import java.util.Arrays;
import java.util.List;

import org.yaac.shared.egql.ResultCell;
import org.yaac.shared.egql.ResultCell.ResultCellFactory;
import org.yaac.shared.util.StringUtils;

import com.google.common.base.Joiner;



/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class UserPropertyInfo extends PropertyInfo {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String authDomain;
	
	private String email;
	
	private String federatedIdentity;
	
	private String userId;
	
	private String nickname;
	
	/**
	 * used by GWT
	 */
	@SuppressWarnings("unused")
	private UserPropertyInfo(){}
	
	public UserPropertyInfo(
			String authDomain, String email, String federatedIdentity, String userId, String nickname) {
		super();
		this.authDomain = authDomain;
		this.email = email;
		this.federatedIdentity = federatedIdentity;
		this.userId = userId;
		this.nickname = nickname;
	}
	
	public String getAuthDomain() {
		return authDomain;
	}

	public String getEmail() {
		return email;
	}

	public String getFederatedIdentity() {
		return federatedIdentity;
	}

	public String getUserId() {
		return userId;
	}

	public String getNickname() {
		return nickname;
	}

	@Override
	public int compareTo(PropertyInfo o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String asHtml() {		
		List<String> lines = newLinkedList();
		lines.add("Email : " + this.email);
		
		if (!isNullOrEmpty(this.authDomain)) {
			lines.add("Auth Domain : " + this.authDomain);
		}
		
		if (!isNullOrEmpty(this.userId)) {
			lines.add("User Id : " + this.userId);
		}
		
		if (!isNullOrEmpty(this.federatedIdentity)) {
			lines.add("Federated Identity : " + this.federatedIdentity);
		}
		
		return Joiner.on("<br/>").join(lines);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((authDomain == null) ? 0 : authDomain.hashCode());
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((federatedIdentity == null) ? 0 : federatedIdentity.hashCode());
		result = prime * result + ((nickname == null) ? 0 : nickname.hashCode());
		result = prime * result + ((userId == null) ? 0 : userId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UserPropertyInfo other = (UserPropertyInfo) obj;
		if (authDomain == null) {
			if (other.authDomain != null)
				return false;
		} else if (!authDomain.equals(other.authDomain))
			return false;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (federatedIdentity == null) {
			if (other.federatedIdentity != null)
				return false;
		} else if (!federatedIdentity.equals(other.federatedIdentity))
			return false;
		if (nickname == null) {
			if (other.nickname != null)
				return false;
		} else if (!nickname.equals(other.nickname))
			return false;
		if (userId == null) {
			if (other.userId != null)
				return false;
		} else if (!userId.equals(other.userId))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "UserPropertyInfo [authDomain=" + authDomain + ", email=" + email + ", federatedIdentity="
				+ federatedIdentity + ", userId=" + userId + ", nickname=" + nickname + "]";
	}

	@Override
	public ResultCell populateResultCell(ResultCellFactory factory) {
		ResultCell cell = super.populateResultCell(factory);
		cell.setType(PropertyType.USER);
		cell.setParent(null);
		cell.setChildren(null);
		cell.setPayload(Arrays.asList(authDomain, email, federatedIdentity, userId, nickname));
		return cell;
	}

	@Override
	public String asExpression(FilePlaceHolder placeHolder) {		
		return "user(" + Joiner.on(",").join(
				StringUtils.wrap(this.email, "'"), 
				StringUtils.wrap(this.authDomain, "'")
				) + ")";
	}

	@Override
	public List<String> downloadPaths() {
		return newArrayList();
	}
}
